<?php

namespace Register\Controller;

class IndexController extends BaseController {

  public function __construct() {
    parent::__construct();
    $B = M("Sys")->where(array("name"=>'currency_name'))->getField("value");
    $wallet_name = M("Sys")->where(array("name"=>"wallet_name"))->getField("value");
    $sysname = M("Sys")->where(array("name"=>"sys_name"))->getField("value");
    $this->assign('wallet_name', $wallet_name);
    $this->assign('sys_name', $sysname);
    $this->assign('B', $B);
    $this->assign('lang', $this->lang("zh"));
  }

  public function index() {
    $reg_on = M("Sys")->where(array("name"=>"reg_on"))->getField("value");
    if($reg_on == 1){
      $this->error('注册已关闭', U('User/Index/login'));
      die;
    }
    $parent = I("parent")?I("parent"): $_SESSION["parent"];
    $_SESSION["parent"] = $parent;
    if($_SESSION["parent"]){
      $parent = $_SESSION["parent"];
      $parentid = M("User")->where(array("username"=>$parent))->getField("id");
      if($parentid){
        $this->assign("parent",$_SESSION["parent"]);
      }else{
        $_SESSION["parent"] = "";
        $this->error('推荐人不存在！', U('Register/Index/index'));
      }
    }
    $this->display();
  }

  public function check(){
    if(IS_GET)
    {
      $user = M("User");
      $uname = I("get.uname");
      if($uname){
        $resuname = $user->where("username = '$uname'")->getField("id");
        if($resuname != ''){
          $this->ajaxReturn('该用户名已被使用','JSON'); /*json方式返回结果*/
        }else{
          $this->ajaxReturn('','JSON'); /*json方式返回结果*/
        }
      }
    }
  }


  /*注册用户*/
  public function insert(){

    $user = M("User");
    $username = I("post.username");
    $password = md5(I("post.password"));
    $parent = !empty(I("post.parent"))?I("post.parent"):'';

    //检测用户是否存在
    $is_user = M("User")->where(array("username"=>$username))->getField("id");
    if($is_user>0){
      $this->error('该用户已存在，请不要重复注册！');
      die;
    }

    $parent1 = M("User")->where(array("username"=>$parent))->getField("id");
    if($parent1 > 0){
      $parent2 = $parent1;
    }else{
      $parent2 = 0;
    }
    $code = I("post.code");
    $em_code = I("post.em_code");
    $isparent = $user->where("id = '$parent2'")->getField("id");
     if(isset($parent2) && empty($isparent)){
        $this->error('推荐人不存在！请重新填写');
        die;
      }
   /* if($code != $_SESSION['code']){
      $this->error('验证码输入错误！');
      die;
    }*/
   /* if($em_code != $_SESSION['em_code']){
      $this->error('您的email安全码错误！');
      die;
    }*/
    $data['username'] = $username;
    $data['password'] = $password;
    $data['user_class'] = 0;
    $data['parent'] = $parent2;
    $data['reg_time'] = time();
    //生成钱包地址
    $qbdz = createnewaccount($username);
    $qbdz['error'] = 0;
    if($qbdz['error'] == 0){
	  $data['qbdz'] = $qbdz['qbdz'];
      $res = $user->add($data);
    }else{
      $this->error('网络错误!请稍后再试！', U('User/Index/login'));
    }
    if($res){
      $_SESSION['em_code'] = '';
      $this->success('注册成功!', U('User/Index/login'));

    }
  }


  /*注册用户*/
  public function webinsert(){
    $username = $_POST['username'];
    $password = md5($_POST['password']);
    $parent = !empty($_POST['parent'])?$_POST['parent']:'';

    //检测用户是否存在
    $is_user = M("User")->where(array("username"=>$username))->getField("id");
    if($is_user>0){
      $return['error'] = 1;
      $return['msg'] = '会员已经存在！';
      echo json_encode($return);
      die;
    }
    $parentid = M("User")->where(array("username"=>$parent))->getField("id");
    if(!$parentid){
      $return['error'] = 2;
      $return['msg'] = '推荐人不存在';
      echo json_encode($return);
    }
    /* if($code != $_SESSION['code']){
       $this->error('验证码输入错误！');
       die;
     }*/
    /* if($em_code != $_SESSION['em_code']){
       $this->error('您的email安全码错误！');
       die;
     }*/
    $data['username'] = $username;
    $data['password'] = $password;
    $data['user_class'] = 0;
    $data['parent'] = $parentid?$parentid:0;
    $data['shiming'] = 1;
    $data['activate_time'] = time();
    $data['reg_time'] = time();
    //生成钱包地址
    $res = M("User")->add($data);
    if($res){
      $_SESSION['em_code'] = '';
      $return['error'] = 0;
      $return['msg'] = '同步成功！';
      echo json_encode($return);
    }
  }

  /*爱心激活券奖励*/
  public function webjl(){
    $username = $_POST['username'];
    $jiang = $_POST['jiang'];
    $user =  M("User")->where(array("username"=>$username))->find();
    $data['usermoney'] = $user['usermoney'] + $jiang;
    $res = M("User")->where(array("username"=>$username))->save($data);//更新用户余额
    if($res){
      add_log($user['id'], $jiang,0, 29, "爱心激活券被使用，奖励".$jiang);
      $return['error'] = 0;
      $return['msg'] = '发放爱心激活券奖励成功！';
      echo json_encode($return);
    }else{
      $return['error'] = 1;
      $return['msg'] = '发放爱心激活券奖励失败！';
      echo json_encode($return);
    }
  }
}
